package roborally;

import be.kuleuven.cs.som.annotate.Basic;

/**
 * An enum EnergyUnit, that holds all possible types of energyunits(JOULE, KILOJOULE, WATTSECOND, KILOWATTHOUR).
 * 
 * @version  2.90
 * @author   Michiel Ruelens & Dieter Naten
 */
public enum EnergyUnit {
	WATTSECOND("Ws"), KILOWATTHOUR("kWh"), JOULE("J"), KILOJOULE("kJ");	//Units for energy
	
	private final String symbol;
	
	/**
	 * This method makes a new energyunit with the given symbol
	 * 
	 * @param symbol
	 * 		  The symbol the new unit should be given
	 * @post  | (new this).getSymbol() == symbol
	 */
	private EnergyUnit(String symbol){
		this.symbol = symbol;
	}
	
	/**
	 * This method returns the current symbol.
	 */
	@Basic
	public String getSymbol() {
		return this.symbol;
	}
	
	/**
	 * Returns the value of another energyunit compared to 1 of this unit.
	 * 
	 * @param 	other
	 * 			The other energyunit that is compared to this one.
	 * @return	| result ==
	 * 			| getEnergyUnitFactor(this, other)
	 */
	public double getEnergyUnitFactor(EnergyUnit other){
		double factor = getEnergyUnitFactor(this, other);
		return factor;	
	}
	
	/**
	 * Get the conversion rate between 2 energyunits. The result is the value of the second energyunit compared to 1 of the first energyunit.
	 * 
	 * @param 	unit1
	 * 			The energyunit to get the conversion rate with unit2.
	 * @param 	unit2
	 * 			The energyunit to get the conversion rate with unit1.
	 * @return  The conversion rate between the two given units.
	 * 			| result == 
	 * 			| factor
	 */
	public static double getEnergyUnitFactor(EnergyUnit unit1, EnergyUnit unit2){
		double factor = 0;
			if(unit1==unit2 || (unit1==EnergyUnit.JOULE && unit2==EnergyUnit.WATTSECOND) 
							|| (unit1==EnergyUnit.WATTSECOND && unit2==EnergyUnit.JOULE)){
				factor = 1;
			}
			else if(unit1==EnergyUnit.JOULE || unit1==EnergyUnit.WATTSECOND){
				if(unit2==EnergyUnit.KILOJOULE){
					factor = 1/1000;
				}
				else if(unit2==EnergyUnit.KILOWATTHOUR){
					factor = 3600000;
				}
			}
			else if(unit1==EnergyUnit.KILOJOULE){
				if(unit2==EnergyUnit.JOULE || unit2==EnergyUnit.WATTSECOND){
					factor = 1000;
				}
				else if(unit2==EnergyUnit.KILOWATTHOUR){
					factor = 3600000*1000;
				}
			}
			else if(unit1==EnergyUnit.KILOWATTHOUR){
				if(unit2==EnergyUnit.JOULE || unit2==EnergyUnit.WATTSECOND){
				factor = 1/3600000;
				}
				else if(unit2==EnergyUnit.KILOJOULE){
					factor = 1/(3600000*1000);
				}
			}
		return factor;
	}
}